package com.shop.shoporder.wapController;

import com.shop.shoporder.common.InterfaceVer;
import com.shop.shoporder.common.MessageConstants;
import com.shop.shoporder.common.Response;
import com.shop.shoporder.common.UserLoginToken;
import com.shop.shoporder.entity.order.Cart;
import com.shop.shoporder.entity.order.Order;
import com.shop.shoporder.entity.order.SureOrder;
import com.shop.shoporder.service.CartService;
import com.shop.shoporder.service.GoodsCommentService;
import com.shop.shoporder.service.OrderService;
import com.shop.shoporder.service.SureOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@Controller
@CrossOrigin(origins = "*",maxAge = 3600)
@RequestMapping("/wap")
public class CreateOrderController {
    @Autowired
    OrderService orderService;
    @Autowired
    GoodsCommentService goodsCommentService;
    @Autowired
    CartService cartService;
//    @UserLoginToken
    @ResponseBody
    @RequestMapping(value = "/addOrder",method = RequestMethod.POST)
    //生成订单
    public Response addOrder(@RequestParam Map<String,String> map, HttpServletRequest request) {
        Response response=new Response();
        try{
             response=orderService.addOrder(map,request);
        }catch (Exception e){
            e.printStackTrace();
            return new Response(MessageConstants.MSG_REGISTER_FAILED,null,0,"获取异常！",request);
        }
        return response;
    }

    @UserLoginToken
    @ResponseBody
    @RequestMapping(value = "/cancelOrder",method = RequestMethod.POST)
    //取消订单
    public Response cancelOrder(@RequestParam Map<String,String> map, HttpServletRequest request) {
        Response response=new Response();
        try{
            response=orderService.cancelOrder(map,request);
        }catch (Exception e){
            e.printStackTrace();
            return new Response(MessageConstants.MSG_SYS_ERROR,null,0,"获取异常！",request);
        }
        return response;
    }

    @UserLoginToken
    @ResponseBody
    @RequestMapping(value = "/sureConfirm",method = RequestMethod.POST)
    //确认收货
    public Response sureConfirm(@RequestParam Map<String,String> map, HttpServletRequest request) {
        Response response=new Response();
        try{
            response=orderService.sureConfirm(map,request);
        }catch (Exception e){
            e.printStackTrace();
            return new Response(MessageConstants.MSG_SYS_ERROR,null,0,"获取异常！",request);
        }
        return response;
    }

    @UserLoginToken
    @ResponseBody
    @RequestMapping(value = "/getOrderCommentGoodsList",method = RequestMethod.GET)
    //评论产品列表
    public Response getOrderCommentGoodsList(@RequestParam Map<String,String> map, HttpServletRequest request) {
        try{
            List<Cart> cartList=cartService.getOrderCommentGoodsList(map,request);
            return new Response(MessageConstants.MSG_SUCCESS,cartList,1,"获取成功!",request);
        }catch (Exception e){
            e.printStackTrace();
            return new Response(MessageConstants.MSG_SYS_ERROR,null,0,"获取异常！",request);
        }
    }

    //@UserLoginToken
    @Transactional
    @ResponseBody
    @RequestMapping(value = "/setPayType",method = RequestMethod.POST)
    //支付方式
    public Response setPayType(@RequestParam Map<String,String> map, HttpServletRequest request) {
        Response response=new Response();
        try{
            int payType = InterfaceVer.VerNumber(request.getParameter("payType")==null?"0":request.getParameter("payType"));
            String orderNo = request.getParameter("orderNo");
            if(payType>0){
                int res= orderService.updatePayType(payType,orderNo);
                if(res>0){
                    return new Response(MessageConstants.MSG_SUCCESS,null,0,"设置成功",request);
                }else {
                    return new Response(MessageConstants.MSG_SYS_ERROR,null,0,"设置失败",request);
                }
            }else {
                return new Response(MessageConstants.MSG_SYS_ERROR,null,0,"支付方式参数异常",request);
            }
        }catch (Exception e){
            e.printStackTrace();
            return new Response(MessageConstants.MSG_SYS_ERROR,null,0,"获取异常！",request);
        }
    }

}
